﻿using System;
using System.Text;
using System.Security.Cryptography;
using System.Security.Authentication;

namespace Bing.Toolkit.Utility
{
    /// <summary>
    /// MD5加密算法
    /// </summary>
    public class MD5
    {
        private static MD5 _instance = new MD5();
        /// <summary>
        /// 当前实例
        /// </summary>
        public static MD5 Instance
        {
            get
            {
                return _instance;
            }
        }

        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="input">要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public string Encrypt(string input)
        {
            return Encrypt(input, "qikancom");
        }
        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="input">要加密的字符串</param>
        /// <param name="key">密钥</param>
        /// <returns>加密后的字符串</returns>
        public string Encrypt(string input, string key)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            string text = input + key;

            byte[] i = md5.ComputeHash(StringHelper.StrEncoding.StrToBytes(text));

            StringBuilder result = new StringBuilder();

            foreach (byte n in i)
            {
                result.Append(n.ToString("X2"));
            }
            return result.ToString();
        }
    }
}
